package com.colin.project.controller;

import com.colin.project.bean.User;
import com.colin.project.jedis.UserJedisOperate;
import com.colin.project.service.UserService;
import com.colin.project.util.EncipherUtil;
import com.colin.project.util.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    //jedis接口，用于生成验证码和校验验证码
    @Autowired
    private UserJedisOperate userJedisOperate;
    //md5加密工具类
    @Autowired
    private EncipherUtil encipherUtil;
    //返回结果枚举
    @Autowired
    private ResponseResult<String> responseResult;

    /**
     * 校验用户名
     * @param username
     * @return
     */
    @RequestMapping("/checkUsername/{username}")
    @ResponseBody
    public ResponseResult<String> checkUsername(@PathVariable("username") String username) {
        return userService.checkUsername(username);
    }

    /**
     * 发送验证码
     * @param email
     * @param flag
     * @return
     */
    @RequestMapping("/sendEmail/{email}/{flag}")
    @ResponseBody
    public ResponseResult<String> sendEmail(@PathVariable("email") String email,
                                            @PathVariable("flag") Integer flag) {
        return userService.sendEmail(email,flag);
    }

    /**
     * 校验验证码
     * @param code
     * @param email
     * @return
     */
    @RequestMapping("/checkCode/{code}/{email}")
    @ResponseBody
    public ResponseResult<String> checkCode(@PathVariable("code") String code,
                                            @PathVariable("email") String email
    ) {
        return userService.checkCode(code,email);
    }

    /**
     * 注册
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("/doRegister")
    public String doRegister(User user, Model model) {
        //控制层接受前端返回的注册成功与否，再通过model返回一个标记位给前端，前端根据标记位判断是否执行定时任务实现页面延迟跳转
        if(!userService.doRegister(user)){
            model.addAttribute("error",null);
            return "register";
        } else {//注册成功向前端传递标记位"success",前端判断接受到的是"success"自动跳转到登录界面
            model.addAttribute("success",true);
            return "login";
        }
    }

    /**
     * 修改密码
     * @param email
     * @param password
     * @param model
     * @return
     */
    @RequestMapping("/changePassword/{email}")
    public String changePassword(@PathVariable("email") String email,
                                 @RequestParam("password") String password,
                                 Model model) {
        if(userService.changePassword(email,password)){
            model.addAttribute("changePasswordSuccess",true);
        } else {
            model.addAttribute("changePasswordError",true);
        }
        return "changePassword";
    }

    /**
     * 登录
     * @param username
     * @param password
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/doLogin")
    public String doLogin(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          HttpSession session,
                          Model model) {
        User user = userService.doLogin(username,password);
        if(user == null){
            model.addAttribute("error",true);
            return "login";
        }
        session.setAttribute("loginUser",user);
        return "redirect:/video/getHomeVideos";
    }

    /**
     * 头像上传
     * @param multipartFile
     * @param md5
     * @param session
     * @return
     */
    @RequestMapping("/headImgUpload")
    @ResponseBody
    public ResponseResult<String> headImgUpload(@RequestParam("headImg") MultipartFile multipartFile,
                                                @RequestParam("md5") String md5,
                                                HttpSession session) {
        return userService.headImgUpload(multipartFile,session,md5);
    }
}
